package userInterface;
import javax.swing.*;

import assembuler.Assembuler;

import java.awt.*;
import java.awt.event.*;
import java.io.*;

public class MainMenuBar extends JMenuBar implements ActionListener,ItemListener{
    public MainMenuBar() {
        JMenuItem menuItem;
        JCheckBoxMenuItem cbMenuItem;

        //File Menu
        JMenu fileMenu = new JMenu("File");
        add(fileMenu);
        menuItem = new JMenuItem("New",KeyEvent.VK_N);
        menuItem.setToolTipText("Create a New Panel");
        menuItem.addActionListener(this);
        fileMenu.add(menuItem);
        menuItem = new JMenuItem("Execute",KeyEvent.VK_X);
        menuItem.setToolTipText("Execute a exist File");
        menuItem.addActionListener(this);
        fileMenu.add(menuItem);
        menuItem = new JMenuItem("Open",KeyEvent.VK_O);
        menuItem.setToolTipText("Open a exist File");
        menuItem.addActionListener(this);
        fileMenu.add(menuItem);
        menuItem = new JMenuItem("Quit",KeyEvent.VK_Q);
        menuItem.setToolTipText("Exit the program");
        menuItem.addActionListener(this);
        fileMenu.add(menuItem);
        
        //OptionMenu
        JMenu optionMenu = new JMenu("Option");
        add(optionMenu);
        cbMenuItem = new JCheckBoxMenuItem("Animation");
        cbMenuItem.addItemListener(this);
        cbMenuItem.setSelected(EnvironmentVariables.haveAnimation);
        optionMenu.add(cbMenuItem);

        //HelpMenu
        JMenu helpMenu = new JMenu("Help");
        add(helpMenu);
        menuItem = new JMenuItem("Help",KeyEvent.VK_H);
        menuItem.setToolTipText("Find help via Internet");
        menuItem.addActionListener(this);
        helpMenu.add(menuItem);
        menuItem = new JMenuItem("About",KeyEvent.VK_A);
        menuItem.addActionListener(this);
        helpMenu.add(menuItem);
    }

	@Override
	public void actionPerformed(ActionEvent e) {
		String cmd=e.getActionCommand();
		if (cmd.equals("Quit")) {
			System.exit(0);
		}
		if (cmd.equals("About")) {
			JOptionPane.showMessageDialog(EnvironmentVariables.mainFrame, "SimplePCLogo v1.0", "About", JOptionPane.INFORMATION_MESSAGE);
		}
		if (cmd.equals("Execute")) {
        	JFileChooser executeFileChooser=new JFileChooser(".");
        	executeFileChooser.setAcceptAllFileFilterUsed(false);
        	executeFileChooser.addChoosableFileFilter(new ImageFileFilter("logo"));
        	int result=executeFileChooser.showOpenDialog(EnvironmentVariables.mainFrame);
        	if (result==JFileChooser.APPROVE_OPTION) {
        		String executeFilePath=executeFileChooser.getSelectedFile().getAbsolutePath();
        		Assembuler.executeCodeFromFile(executeFilePath);
        	}
		}
		if (cmd.equals("New")) {
			EnvironmentVariables.editorList.add(new LogoScriptEditorFrame(String.format("LogoScript %d", EnvironmentVariables.editorList.size()+1),null));
		}
		if (cmd.equals("Open")) {
			File filePath;
        	JFileChooser openFileChooser=new JFileChooser(".");
        	openFileChooser.setAcceptAllFileFilterUsed(false);
        	openFileChooser.addChoosableFileFilter(new ImageFileFilter("logo"));
        	int result=openFileChooser.showOpenDialog(EnvironmentVariables.mainFrame);
        	if (result==JFileChooser.APPROVE_OPTION) {
        		filePath=openFileChooser.getSelectedFile();
        		EnvironmentVariables.editorList.add(new LogoScriptEditorFrame(filePath.getName(),filePath));
        	}
		}
		if (cmd.equals("Help")) {
			Helper.printHelpMessage();
		}

	}

	@Override
	public void itemStateChanged(ItemEvent e) {
		JCheckBoxMenuItem cbmi=(JCheckBoxMenuItem)e.getSource();
		if (!EnvironmentVariables.mainPanel.logoIsBusy) {
			EnvironmentVariables.haveAnimation=cbmi.isSelected();
		} else {
			System.err.println("Logo is Busy,You can't change this Option!");
			cbmi.setSelected(EnvironmentVariables.haveAnimation);
		}
	}

}
